/**
 * Created by limeng on 16-4-26.
 */
delegateApp.controller('MyProposalCtrl', function ($scope, $rootScope, $http) {
    $scope.searchMap.authorId = $rootScope.delegate.id;
    $scope.searchMap.proposalState = null;

    $scope.showDeleteBtn = function (proposal) {
        if (proposal && proposal.proposalState) {
            switch (proposal.proposalState) {
                case 'DRAFT':
                case 'UNCHECKED':
                case 'WAIT_SECOND_MOTION':
                case 'UNREGISTERED':
                case 'INVALID':
                    return true;
                default :
                    return false;
            }
        }
        return false;
    };

    $scope.showUpdateBtn = function (proposal) {
        if (proposal && proposal.proposalState) {
            switch (proposal.proposalState) {
                case 'DRAFT':
                case 'UNCHECKED':
                case 'WAIT_SECOND_MOTION':
                case 'UNREGISTERED':
                    return true;
                default :
                    return false;
            }
        }
        return false;
    };

    $scope.deleteProposal = function (proposal) {
        var proposalId = proposal.id;
        bootbox.confirm("真的要删除这个提案吗?", function (res) {
            if (res) {
                $http.delete("/proposal/"+proposalId).success(function (data) {
                    if (data.state) {
                        bootbox.alert("删除成功!");
                        $scope.updateContent();
                    }
                }).error(function() {
                    bootbox.alert("服务器没有响应!");
                });
            }
        });
    };
    $scope.proposalFeedback = function (proposal) {
        if (!proposal.feedbackInfo && proposal.feedbackInfo != '') {
            return;
        }
        var proposalId = proposal.id;
        bootbox.prompt('请输入反馈信息：', function (result) {
            if (result != null) {
                $http.post('/proposal/delegateFeedback/' + proposalId, result)
                    .success(function (data) {
                        if (data.status == true) {
                            bootbox.alert("反馈已上传！");
                        } else {
                            bootbox.alert("操作失败，原因是" + data.result);
                        }
                    }).error(function () {
                        bootbox.alert("服务器遇到故障！")
                    });
            }
        });
    }
});